/*******************************************
  Jose Andres Lara Vecino

  Author       : $$Author: cainlara@gmail.com $$
  Last CheckIn : $$Date: 2012-10-09 15:00:35 +0000 (Tue, 09 Oct 2012) $$
  Revision     : $$Revision: 107 $$
*******************************************/

package generico;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import core.TestManagerException;

/**
 * Super clase para la capa DAO.
 * 
 * @author jose.lara 07.06.2012
 *
 * @param <T> La clase para la cual se implementa el DAO.
 */
public class GenericoDao<T> implements Serializable {
    private static final long serialVersionUID = -5872840835360492640L;
    
    private static volatile EntityManagerFactory emf    = null;
    private static volatile EntityManager em            = null;
    
    public GenericoDao() {
    }
    
    @SuppressWarnings("unchecked")
    protected List<T> getResultFindAll(String nombreEntidad) throws TestManagerException {
        Query query = null;
        try {
            final String queryString = "select model from " + nombreEntidad + " model";
            query = getEntityManager().createQuery(queryString);
            return query.getResultList();
        }
        catch (RuntimeException re) {
            throw new TestManagerException(re);
        }
    }
    
    public T getResultfindById(Class<T> entityClass, Long primaryKey) {
        return getEntityManager().find(entityClass, primaryKey);
    }
    
    private static EntityManagerFactory getEntityManagerFactory() throws TestManagerException {
        if(emf == null) {
            emf = Persistence.createEntityManagerFactory("testManagerPU");
        }
        
        return emf;
    }
    
    protected static EntityManager getEntityManager() throws TestManagerException {
        if(em == null) {
            em = getEntityManagerFactory().createEntityManager();
        }
        
        return em;
    }
    
    @SuppressWarnings("unchecked")
    protected T getUniqueResultFindByPropertie(String propertyName, final Object value, String tabla) throws TestManagerException {
        T objeto            = null;
        Query query         = null;
        String queryString  = null;
        
        queryString = "select model from " + tabla + " model where model." + propertyName + "= :propertyValue";
        
        query = getEntityManager().createQuery(queryString);
        query.setParameter("propertyValue", value);
        objeto = (T) query.getSingleResult();
        
        return objeto;
    }
    
    public void save(T entity) throws TestManagerException {
        getEntityManager().getTransaction().begin();
        getEntityManager().persist(entity);
        getEntityManager().getTransaction().commit();
    }
    
    public void delete(T entity) throws TestManagerException {
        getEntityManager().getTransaction().begin();
        getEntityManager().remove(entity);
        getEntityManager().getTransaction().commit();
    }
}